//
//  WBVisitorView.swift
//  仿写微博
//
//  Created by Apple on 17/7/10.
//  Copyright © 2017年 tuimao. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    //访客视图信息字典,重写set方法
    //首页传""值，不能穿nil
    var visitorInfor:[String:String]?{
        didSet{
            guard let imageName = visitorInfor?["imageName"],let message = visitorInfor?["message"] else {
                return
            }
            tipLabel.text = message
            if imageName == "" {
                startAnimation()
                return
            }
            iconView.image = UIImage(named: imageName)
            //其他控制器不需要 house和maskIcon隐藏
            hourseIconView.isHidden = true
            maskIconView.isHidden = true
         }
    }

    //初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置UI
        setupUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //iconView的旋转动画
    func startAnimation(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * Double.pi
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        //动画完成时候不删除,只有当iconView销毁的时候，才被释放
        anim.isRemovedOnCompletion = false
        //添加到图层
        iconView.layer.add(anim, forKey: nil)
        
    }
    
    //设置私有控件
    //图像视图
    fileprivate lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //添加遮盖图像
    fileprivate lazy var maskIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    // 小房子
    fileprivate lazy var hourseIconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //提示标签
    fileprivate lazy var tipLabel:UILabel = UILabel.cz_label(withText: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜", fontSize: 14, color: UIColor.darkGray)
    //注册按钮
     lazy var registButton:UIButton = UIButton.cz_textButton("注册", fontSize: 16, normalColor: UIColor.orange, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    //登陆按钮
     lazy var loginButton:UIButton = UIButton.cz_textButton("登陆", fontSize: 16, normalColor: UIColor.darkGray, highlightedColor: UIColor.black, backgroundImageName: "common_button_white_disable")
    
    
}


//MARK:UI布局
extension WBVisitorView{
    
     fileprivate func setupUI(){
        backgroundColor = UIColor.cz_color(withHex: 0xEDEDED)
        //添加到视图
        addSubview(iconView)
        addSubview(maskIconView)
        addSubview(hourseIconView)
        addSubview(tipLabel)
        addSubview(registButton)
        addSubview(loginButton)
        //居中显示文字
        tipLabel.textAlignment = .center
        //取消autoresizeing布局
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        //自动布局
        let margin:CGFloat = 20.0
        
        let iconViewConstrain1 = NSLayoutConstraint(item: iconView,
                                                    attribute: .centerX,
                                                    relatedBy: .equal,
                                                    toItem: self,
                                                    attribute: .centerX,
                                                    multiplier: 1,
                                                    constant: 0)
        let iconViewConstrain2 = NSLayoutConstraint(item: iconView,
                                                    attribute: .centerY,
                                                    relatedBy: .equal,
                                                    toItem: self,
                                                    attribute: .centerY,
                                                    multiplier: 1,
                                                    constant: -60)
        //添加图像视图布局
        addConstraints([iconViewConstrain1,iconViewConstrain2])
        //小房子视图布局
        let hourseIconViewConstrain1 = NSLayoutConstraint(item: hourseIconView,
                                                          attribute: .centerX,
                                                          relatedBy: .equal,
                                                          toItem: iconView,
                                                          attribute: .centerX,
                                                          multiplier: 1,
                                                          constant: 0)
        let hourseIconViewConstrain2 = NSLayoutConstraint(item: hourseIconView,
                                                          attribute: .centerY,
                                                          relatedBy: .equal,
                                                          toItem: iconView,
                                                          attribute: .centerY,
                                                          multiplier: 1,
                                                          constant: 0)
        addConstraints([hourseIconViewConstrain1,hourseIconViewConstrain2])
        //提示标签布局
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .centerX,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: tipLabel,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: 236))
        //注册按钮布局
        addConstraint(NSLayoutConstraint(item: registButton,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .left,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: registButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: registButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1,
                                         constant: 100))
        //登录按钮的布局
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .right,
                                         multiplier: 1,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLabel,
                                         attribute: .bottom,
                                         multiplier: 1,
                                         constant: margin))
        addConstraint(NSLayoutConstraint(item: loginButton,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registButton,
                                         attribute: .width,
                                         multiplier: 1,
                                         constant: 0))
        //遮盖视图
        let viewDict = ["maskIconView":maskIconView,"registButton":registButton] as [String : Any]
        let metrics = ["space":20]
        //水平方向布局
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: viewDict))
        //垂直方向布局
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[maskIconView]-(space)-[registButton]", options: [], metrics: metrics, views: viewDict))
        
        
        }
}
